package com.liu.system.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liu.common.utils.IdWorker;
import com.liu.system.manage.domain.DRole;
import com.liu.system.manage.mapper.RoleMapper;
import com.liu.system.manage.service.IRoleService;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
@Transactional
public class RoleServiceImpl extends ServiceImpl<RoleMapper, DRole> implements IRoleService {

    @Autowired
    RoleMapper roleMapper;

    @Autowired
    IdWorker idWorker;


    @Override
    public Page<DRole> queryUserForPage(int page, int limit, String query) {
        Page<DRole> rolepages = new Page<>(page,limit);
        QueryWrapper<DRole> wrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(query)) {
            wrapper.like("name", query).or().like("description", query);
        }
        rolepages = (Page<DRole>) roleMapper.selectPage(rolepages, wrapper);

        log.info("page：" + page);
        log.info("rows：" + limit);
        log.info("query：" + query);

        log.info("总数：" + rolepages.getTotal());
        log.info("总页数：" + rolepages.getPages());

        return rolepages;
    }

    @Override
    public int updateRole(DRole role) {
        role.setCreateTime(null);
        return roleMapper.updateById(role);
    }

    @Override
    public int addRole(DRole role) {
        role.setId(idWorker.nextId()+"");

        return roleMapper.insert(role);
    }

    @Override
    public int delete(String id) {
        return roleMapper.deleteById(id);
    }
}
